var global = require("Global")
var dataDao = require("DataDao");
var AU = require("AudioUtils").inst;
cc.Class({
    extends: cc.Component,

    /*
    玩家组件
    */
    properties: {
       
        weight: {//
            // ATTRIBUTES:
            default: 0,        // The default value will be used only when the component attaching
            type: cc.Integer, // optional, default is typeof default
            serializable: true,   // optional, default is true
        },
        weightExtra :0.0,
        weightLabel:{
         // ATTRIBUTES:
         default: null,        // The default value will be used only when the component attaching
         type: cc.Label,// optional, default is typeof default
         serializable: true,   // optional, default is true
        },
        
        dir: cc.v2(0, 0),//移动目标位置
        isTouch:false,
  
        startPos: cc.v2(0, 0),//按下记录位置
        gameOver: { //gameover 文字节点
            default: null,
            type: cc.Node
        },
        gameCamera:{ //游戏相机
            default: null,
            type: cc.Node
        },
      
        gameLogic://游戏根节点
        {
            default: null,
            type: cc.Node
        },

        buffer:null,//默认英雄带有所有buffer 只是未启用
        skill :null,
    },

    // use this for initialization
    onLoad: function () {

        this.init();        

        
    },
    init:function()
    {
        this.touchingNumber = 0;
        this.buffer = require("Buffer").buffer;
        this.skill = require("Skill").skill;
        //激活buffer
        for(var i=0;i<global.heroBuffer.length;i++)
        {
        //    this.buffer[global.heroBuffer[i]].start();
        }
        //带入技能
        var heroSkill = dataDao.getHeroSkill();
        for(var i=0;i<heroSkill.length;i++)
        {
            var skillId =dataDao.playSkill[i];
            var skillLevel =heroSkill[i];
            this.skill[skillId].active = true;
            var skillCfg = global.getSkillUp(skillId);

            this.skill[skillId].effectValue = skillCfg.bufferEffect(skillCfg,skillLevel);
            console.log("skillCfg,skillLevel" +skillId +"   " + this.skill[skillId].effectValue);
            
            this.skill[skillId].allCdTime = skillCfg.cdTime(skillCfg,skillLevel);
            this.skill[skillId].cdTime = 0;
        }
        this.updateAutoSkill(0.01,this);

        var scale2 = dataDao.getPlayer();
         this.weight = dataDao.getWeight();
        //this. weightLabel.string = ""+this.getWeight();
        //this.node.scale = cc.v2(scale2,scale2);

    },
    

    getWeight()
    {
        return this.weight +this.weightExtra;
    },
  
    //碰撞
    onCollisionEnter: function (other, self) {
        var vx = other.getComponent("ActorConfig");
       // AU.playEffect(EFFECT_BOOM1,false);
        if(vx !=null &&vx.weight < this.getWeight() )
        {
            //吃 
           other.node.active = false;
           return;
        }

        if(this.buffer[0].using)//隐身中
        {
            return;
        }

        if(this.buffer[1].using)//概率碰撞
        {
            if(Math.random()<this.buffer[1].effectValue)
            {
                   other.node.active = false;
                return;
            }
        }

        if(this.buffer[2].using)//格挡伤害
        { 
            this.buffer[2].using = false ;
           return ;
        }

        this.node.color = cc.Color.RED;
        
        this.touchingNumber ++;
        
        //this.gameOver.active = true;

        this.gameLogic.getComponent("GameLogic").gameOver();
        
    },
    
    onEnable: function () {
        cc.director.getCollisionManager().enabled = true;
        this.node.color = cc.Color.WHITE;

   //     cc.director.getCollisionManager().enabledDebugDraw = true;
    },

    onDisable: function () {
        cc.director.getCollisionManager().enabled = false;
    //    cc.director.getCollisionManager().enabledDebugDraw = false;
    },
    onCollisionStay: function (other, self) {
    
        if (this.collisionY === -1) {
            if (other.node.group === 'Platform') {
                var motion = other.node.getComponent('PlatformMotion');
                if (motion) {
                    this.node.x += motion._movedDiff;
                }
            }

            
        }
    },
    
    onCollisionExit: function (other) {
        this.touchingNumber --;
        
        if (this.touchingNumber === 0) {
            this.node.color = cc.Color.WHITE;
        }

        this.gameOver.active = false;
    },

    SetDir:function(onTouchCtrl) //计算角色目标位置
    {
        
        this.dir.x =  this.startPos.x+ onTouchCtrl.moveTouchDir.x - onTouchCtrl.startTouchPos.x;
        this.dir.y =  this.startPos.y+ ( onTouchCtrl.moveTouchDir.y - onTouchCtrl.startTouchPos.y);
        this.isTouch = onTouchCtrl.isMoving;
    },
    SetStart:function()//记住按下的角色位置
    {
        this.startPos.y = this.node.y; 
        this.startPos.x = this.node.x;
    },
    
    update: function (dt) {
        this.node.x = this.node.x + (this.dir.x - this.node.x)*0.5; //渐变移动
        this.node.y = this.node.y + (this.dir.y - this.node.y)*0.5; 
        var height =cc.winSize.height;//推荐  原因  短 //  this.canvasGame.getComponent(cc.Canvas).designResolution.height;
        height = height - this.node.height ;
        if(this.node.y >  + height/2)
        {
            this.node.y = + height/2;
        }
        if(this.node.y <  - height/2)
        {
            this.node.y =  - height/2;
        }
        if(this.node.x < -320)
        {
            this.node.x = -320;
        }

        
        if(this.node.x > 320)
        {
            this.node.x = 320;
        }
        var self = this;

        this.updateBuferSkill(dt,self);
        this.updateAutoSkill(dt,self);
        //this. weightLabel.string = ""+this.getWeight();

        //显示效果
        this.bufferEffect(self);
    },

    updateBuferSkill(dt,self){
        for(var i = 0;i < self.buffer.length;i++)
        {
            self.buffer[i].update(dt);
        }

        for(var i = 0;i < self.skill.length;i++)
        {
            self.skill[i].update(dt);
        }
    },


    
    

    updateAutoSkill(dt,self)//
    {

        for(var i = 0;i < self.skill.length;i++)
        {
           if( self.skill[i].active &&  self.skill[i].isNotCd()
           )
           {
            
               self.skill[i].start(self);
           }
        }
    },

    addBuffer(bufferId,effectValue)
    {
        this.buffer[bufferId].effectValue = effectValue;
        this.buffer[bufferId].start();
        
        //this.bufferEffect();

        //挂在bufferId特效 特效自动消失时
    },

    bufferEffect(self)
    {
        if(self.buffer[0].using)
        {

            self.node.opacity = 100;
        }else
        {
            self.node.opacity = 255;
        }
    },

    doBuffer1(effectValue )
    {
         global.heroBuffer[1].effectValue = effectValue;
         global.heroBuffer[1].start();
    },
    setScale(self, scale2)
    {
        console.log("-----------------------------------scale2:" +scale2);
        self.node.scale = scale2;
    },

    SetWeightExtra(self, extra )
    {
        console.log("extra:" +extra);
        
        self.weightExtra = extra;
        //this. weightLabel.string = ""+this.getWeight();
    }




});
